import QtQuick 1.1
import com.nokia.meego 1.0
import "getjson.js" as GetJson
import "getcoments.js" as GetCom
import "WebRequest.js" as Web
Page {
    width: parent.width
    height: parent.height
    lockInPortrait: true
    orientationLock: PageOrientation.LockPortrait
    tools:
        ToolBarLayout {

              ToolIcon {
                  platformIconId: "toolbar-back"

                  onClicked:  appWindow.pageStack.pop();
              }
          }


    //定义变量

    property bool hasmore:true
    property bool loading: false
    property string firsturl: "http://m2.qiushibaike.com/article/list/latest?count=30&page=1"

    Item{
        width:children.width
        height: children.height
        anchors.verticalCenter:  parent.verticalCenter
        anchors.horizontalCenter: parent.horizontalCenter
        visible: loading
        id: indicator1
    BusyIndicator {
                    id:indic
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.left: parent.left
                    anchors.leftMargin: -40
                    running: true
             }
    Label{
        anchors.verticalCenter: parent.verticalCenter
        anchors.left: indic.right
        anchors.leftMargin: 10
        platformStyle: LabelStyle{
        textColor:appWindow.labelcolor
        }

        text:"loading..."
    }

    }


    //定义页面



    //定义内容


        ListModel {  id:listModel }
        ListView
            {


                id:view
                property bool __allowRefresh: false
                property int sumcount:2000
                property bool __loadDone: !(sumcount>listModel.count)
                property bool loadMoreFlag: false

                anchors.fill:parent
                model : listModel
                delegate:maincontent

                header: Component {
                       id: listHeader

                       Item {
                           width: view.width; height: 0

                           Text {
                               id: refreshTip
                               anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: refreshImage.verticalCenter }
                               color: "grey"
                               font.pixelSize: 25
                               text: view.__allowRefresh ? "释放立即刷新" : "下拉可以刷新"
                           }

                           Image {
                               id: refreshImage
                               anchors { bottom: parent.top; bottomMargin: 10; right: refreshTip.left; rightMargin: 50 }
                               source:  "images/pull-arrow.png"
                               rotation: {
                                   var coordY = view.visibleArea.yPosition * Math.max(view.height, view.contentHeight);
                                   if (coordY < -100 || view.__allowRefresh) {
                                       view.__allowRefresh = true;
                                       return -180;
                                   }
                                   else {
                                       return 0;
                                   }
                               }
                               Behavior on rotation { NumberAnimation { duration: 150 } }
                           }
                       }
                   }



                footer: Component {
                       Rectangle {
                           id: loadMoreTip

                           width: parent.width; height: 60
                           color: "transparent"
                           visible: view.model.count > 0

                           Text {
                               id: tipBody
                               anchors.centerIn: parent
                               color: "grey"
                               font.pixelSize: 25
                               text: view.__loadDone ? "全部加载完毕" : (view.loadMoreFlag ? "加载中" : "加载更多")
                           }

                           BusyIndicator {
                               anchors { verticalCenter: parent.verticalCenter; right: tipBody.left; rightMargin: 15 }
                               platformStyle : BusyIndicatorStyle {
                                   size: "medium"
                               }
                               running: true
                               visible: view.loadMoreFlag
                           }
                       }
                   }

                   onMovementEnded: {
                       if (view.atYBeginning) {
                           if (view.__allowRefresh) {
                               view.__allowRefresh = false;
                               mainPage.refresh();
                           }
                       }
                       else if (view.atYEnd) {
                           var count = listModel.count;
                           if (view.model.count > 0 && !view.__loadDone) {
                               mainPage.loadMore();
                           }
                       }
                   }

            }

            ContentCom{
                id:maincontent

            }

            Button {
                        id: testbtn
                        width: 100
                        height: 61
                        visible: listModel.count>10
                        anchors.right: parent.right
                        anchors.bottom: parent.bottom
                        anchors.bottomMargin: 100
                        //anchors.verticalCenter:  parent.verticalCenter
                        //anchors.horizontalCenter: parent.horizontalCenter
                       // iconSource:"images/up2date.png"
                        text: "回顶部"
                        opacity: 0.560
                        onClicked: {
                               //  console.log("currentIndex = :" +view.currentIndex)
                                   //      view.currentIndex =0;
                            view.positionViewAtIndex(0,ListView.Beginning)
                           // console.log("currentIndex = :" +view.currentIndex);
                                   // listview1.currentIndex = 0;
                                     }
            }



         Rectangle{
             id :imageview
             visible: false;
             anchors.verticalCenter:  parent.verticalCenter
             anchors.horizontalCenter: parent.horizontalCenter
             height: children.height>600?600:children.height;

             width: children.width>480?480:children.width;
             opacity: 1


             Flickable {
                 height: imageid.height>500?500:imageid.height;
                 width: imageid.width>480?480:imageid.width;
                 anchors.verticalCenter:  parent.verticalCenter
                 anchors.horizontalCenter: parent.horizontalCenter

                  contentWidth: imageid.width; contentHeight: imageid.height

                  Image {
                      anchors.verticalCenter:  parent.verticalCenter
                      anchors.horizontalCenter: parent.horizontalCenter
                      id :imagesmall;
                      source: "images/thumb_pic.png"
                      ProgressBar {
                          id: pb2
                          width:parent.width
                          //height:20
                          indeterminate: true
                          anchors.verticalCenter:  parent.verticalCenter
                          anchors.horizontalCenter: parent.horizontalCenter
                          //visible: simpletimer.running
                      }
                      MouseArea{
                          anchors.fill: parent;
                          onClicked: {
                               imageview.visible =false;
                          }
                      }



                  }

                  Image {
                 anchors.verticalCenter:  parent.verticalCenter
                 anchors.horizontalCenter: parent.horizontalCenter
                 id :imageid;
                 onStatusChanged: if (status===Image.Ready){

                                      imagesmall.source = ""
                                      imagesmall.visible = false
                                  }
                 MouseArea{
                     anchors.fill: parent;
                     onClicked: {
                          imageview.visible =false;
                     }
                 }

                 source: ""

             }
             }
         }
        Rectangle{
            id:qiushi
            visible: false
            anchors.verticalCenter:  parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            width: 400
            height: children.height
        Text {
              id:qiushitext
             font.pointSize: 24
            color:appWindow.contntcolor
             anchors.top :parent.top
             wrapMode:Text.WordWrap
             width: parent.width
             }
        Button
               {
                   width:150
                   anchors.top: qiushitext.bottom
                   anchors.horizontalCenter: parent.horizontalCenter
                   text:"再试试？"
                   onClicked: {qiushi.visible = false;GetJson.load("");}

                }

    }
        function loadMore()
        {

            GetJson.getmore()
        }

        function refresh(){
            //console.log("i am runing!!!!!!!!");
            listModel.clear();
            GetJson.resfer("");
        }

        ScrollDecorator {
            flickableItem: view
        }
            Component.onCompleted:{
                indicator1.visible=true; GetJson. loadmine()}
}
